import { Context, Next } from 'koa';
import ErrorService from '../../service/Admin/Error.service';
class ErrorController { 
     // 获取报警列表
    async getErrorList(ctx:Context) {
        let pageSize: any = ctx.request.query.pageSize || 10
        let page: any = ctx.request.query.page || 1
        let queryDate: any = ctx.request.query.queryDate || ''
        let queryType: any = ctx.request.query.queryType || ''
        let keywords: any = ctx.request.query.keywords || ''
        let gateway_ip: any = ctx.request.query.gateway_ip || '' // 新增网关IP参数
    
        let list = await ErrorService.getErrorList(page, pageSize, queryDate, queryType, keywords, gateway_ip)
        ctx.body = {code: 200, msg: "获取数据成功", data: list}
    }

    // 添加报警信息
    async addErrorList(ctx:any) {
       let module_id: number = ctx.request.body.module_id //模块编号
        let info: string = ctx.request.body.info //报警内容
        let level: number = ctx.request.body.level //报警等级
        let date: string = ctx.request.body.date // 字符串
        let state: number = ctx.request.body.state // 状态
        let error_value: number = ctx.request.body.error_value //报警的值
        let type_id: number = ctx.request.body.type_id // 类型
        let gateway_ip: string = ctx.request.body.gateway_ip // 新增网关IP参数
        let gateway_name: string = ctx.request.body.gateway_name // 网关名称
        if (!gateway_ip) {
            ctx.body = {
                code: 400,
                msg: "参数错误，需要提供gateway_ip"
            };
            return;
        }
        
        let res = await ErrorService.addErrorInfo(module_id, info, level, date, state, error_value, type_id, gateway_ip,gateway_name)
        
        if (res) {
            ctx.body = {code: 200, msg: "添加报警信息成功"}
        } else {
            ctx.body = {code: 304, msg: "添加失败"}
        }

    }
    // 删除报警信息
    async removeErrorInfo(ctx:Context) {
        let rid:any = ctx.request.query.rid
        let res: number = await ErrorService.removeErrorRecord(rid)
        if (res) {
            ctx.body = {code: 200,msg: "删除成功"}
        } else {
            ctx.body = {code: 304,msg: "删除失败"}
        }
    }

    // 修改状态
    async updateErrorState(ctx:any) {
        let state: number = ctx.request.body.state
        let rid: number = ctx.request.body.rid
        let result = await ErrorService.updateErrorState(rid,state)
        if (result) {
            ctx.body = {code: 200,msg: "修改状态成功"}
        } else {
            ctx.body = {code: 500,msg: "修改失败"}
        }
    }

    async getErrorNum(ctx:Context) {
        let {today,week,month}= ctx.query
        let result = await ErrorService.getErrorNum(today as string,week as string,month as string)
        ctx.body = {
            code: 200,
            msg: "获取成功",
            data: result
        }
    }
  // 新增：获取网关下子设备模块类型报警统计
    async getErrorStatsByModule(ctx: Context) {
        let gateway_ip: any = ctx.request.query.gateway_ip
        let type_id: any = ctx.request.query.type_id
        let module_id: any = ctx.request.query.module_id
        // 验证必需参数
        if (!gateway_ip || !module_id) {
            ctx.body = {
                code: 400,
                msg: "参数错误，需要提供gateway_ip"
            }
            return
        }
        
        try {
            let result = await ErrorService.getErrorStatsByModule(
                gateway_ip,
                type_id ? parseInt(type_id) : undefined,
                module_id ? parseInt(module_id) : undefined
            )
            
            ctx.body = {
                code: 200,
                msg: "获取报警统计成功",
                data: result
            }
        } catch (err) {
            console.log('getErrorStatsByModule controller error:', err)
            ctx.body = {
                code: 500,
                msg: "获取报警统计失败"
            }
        }
    } 

    // 根据类型查询报警统计
    async getErrorStatsByType(ctx: Context) {   
        let type = ctx.query.type || 0;
        let gateway_ip: any = ctx.query.gateway_ip || ''
        if (!type) return ctx.body = {code: 400, msg: "没有传入类型参数"}

        try {
            let data = await ErrorService.getErrorStatsByType(Number(type),gateway_ip)
            ctx.body = {
                code: 200,
                msg: "获取报警统计成功",
                data
            }
        } catch (error) {
            ctx.body = {
                code: 500,
                msg: "获取报警统计失败"
            }
        }
    }

    // 更加网关ip地址然后获取报警信息
    async getErrorStatsByGatewayIp(ctx: Context) { 
        try {
            let data = await ErrorService.getErrorStatsByGatewayIp()
            ctx.body = {
                code: 200,
                msg: "获取报警统计成功",
                data
            }
        } catch (error) {
            ctx.body = {
                code: 500,
                msg: "获取报警统计失败"
            }
        }
    }
}

export default new ErrorController();